<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\kuaishou;

use app\enum\KsCodeEnum;
use app\enum\KuaiShouUrlEnum;
use app\service\BaseService;
use proxy\Client;

class MaterialService extends BaseService
{
    /**
     * 获取图片素材列表.
     * @param array $headers 头信息
     * @param array $params 参数
     * @return bool|mixed|string
     */
    public static function getPicList(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = KuaiShouUrlEnum::getServiceName('material');
            $serviceUrl = KuaiShouUrlEnum::getServiceUrl('material', 'pic_list');

            $res = $client->getJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
            if (intval($res['code']) != KsCodeEnum::SUCCESS) {
                return self::setError($res['message']);
            }

            return $res;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 获取视频素材列表.
     * @param array $headers 头信息
     * @param array $params 参数
     * @return bool|mixed|string
     */
    public static function getVideoList(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = KuaiShouUrlEnum::getServiceName('material');
            $serviceUrl = KuaiShouUrlEnum::getServiceUrl('material', 'video_list');

            $res = $client->getJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
            if (intval($res['code']) != KsCodeEnum::SUCCESS) {
                return self::setError($res['message']);
            }

            return $res;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }


    /**
     * 上传视频.
     * @param array $headers 头部
     * @param array $params 参数
     * @return bool|mixed|string
     */
    public static function videoUpload(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = KuaiShouUrlEnum::getServiceName('material');
            $serviceUrl = KuaiShouUrlEnum::getServiceUrl('material', 'video_upload');
            $res = $client->post($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
            if (intval($res['code']) != KsCodeEnum::SUCCESS) {
                return self::setError($res['message']);
            }
            return $res;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }



    /**
     * 获取可选的推荐封面.
     * @param array $headers 头信息
     * @param array $params 参数
     * @return bool|mixed|string
     */
    public static function getVideoKeyFrame(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = KuaiShouUrlEnum::getServiceName('material');
            $serviceUrl = KuaiShouUrlEnum::getServiceUrl('material', 'video_key_frame');

            $res = $client->getJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
            if (intval($res['code']) != KsCodeEnum::SUCCESS) {
                return self::setError($res['message']);
            }

            return $res;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 视频关联创意数查询.
     * @param array $headers 头信息
     * @param array $params 参数
     * @return bool|mixed|string
     */
    public static function getVideoCreativeNum(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = KuaiShouUrlEnum::getServiceName('material');
            $serviceUrl = KuaiShouUrlEnum::getServiceUrl('material', 'video_creative_num');

            $res = $client->postJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
            if (intval($res['code']) != KsCodeEnum::SUCCESS) {
                return self::setError($res['message']);
            }

            return $res;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }
}
